<template>
  <div class="page-container">
    <div class="arcticle-list" v-if="longArticleList.length > 0">
      <div class="arcticle-item" v-for="item in longArticleList" :key="item.id"
        :style="{ 'background-image': `url(${item.cover})` }" @click="handleArticleClick">
        <span v-html="item.finished == true ? '# 已完结' : '# 连载中'"></span>

        <div class="arcticle-title">《{{ item.serial_title }}》</div>

        <div class="arcticle-msg">
          <div class="arcticle-forward">{{ item.forward }}</div>
          <div class="arcticle-author">{{ item.subtitle }}</div>
        </div>
      </div>
    </div>

    <div v-else class="loading-text">
      加载中...
    </div>
  </div>
</template>

<script>
import { getLongArticleByYear } from '@/api/api'
export default {
  data() {
    return {
      longArticleList: [],
      isLoading: true
    }
  },
  methods: {
    handleArticleClick() {
      this.$toast({
        message: '尽请期待',
        position: 'bottom'
      });
    },
    fetchYearData(year) {
      return getLongArticleByYear(year)
        .then(res => {
          if (res.data && res.data.length > 0) {
            this.longArticleList = [...this.longArticleList, ...res.data];
          }
        })
        .catch(err => {
          console.error(`获取${year}年数据失败:`, err);
        });
    },
    async loadAllYearData() {
      this.isLoading = true;
      const startYear = 2024;
      const endYear = 2016;

      for (let year = startYear; year >= endYear; year--) {
        await this.fetchYearData(year);
      }

      this.isLoading = false;
    }
  },
  created() {
    this.loadAllYearData();
  }
}
</script>

<style scoped lang="less">
.page-container {
  padding: 10vh 10px 7vh;
  box-sizing: border-box;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 5vh;
  background-color: #fff;
  min-height: 100vh;
}

.arcticle-list {
  width: 100%;
}

.arcticle-item {
  width: 100%;
  height: 23vh;
  margin-bottom: 20px;
  background-size: cover;
  background-repeat: no-repeat;
  background-position: bottom;
  border-radius: 8px;
  position: relative;
  box-shadow: 0 2px 4px #0000001a;

  >span {
    position: absolute;
    top: 5px;
    left: 5px;
    font-size: 8px;
    color: #fff;
    padding: 2px 5px;
  }

  .arcticle-title {
    position: absolute;
    left: 5px;
    bottom: 28%;
    font-size: 13px;
    font-weight: 300;
    color: #fff;
    z-index: 2;
  }

  .arcticle-msg {
    width: 100%;
    height: 23%;
    padding: 5px 10px;
    padding-right: 5px;
    box-sizing: border-box;
    position: absolute;
    bottom: 0;
    left: 0;
    background-color: #fff;
    border-bottom-left-radius: 8px;
    border-bottom-right-radius: 8px;
    display: flex;
    flex-direction: column;
    justify-content: space-between;

    &::after {
      content: '';
      position: absolute;
      top: 0;
      left: 0;
      transform: translateY(-100%);
      z-index: 1;
      width: 100%;
      height: 100%;
      background: linear-gradient(to top, rgba(0, 0, 0, 0.315) 0%, rgba(0, 0, 0, 0) 100%);
    }

    .arcticle-title {
      font-size: 12px;
    }

    .arcticle-forward {
      font-size: 10px;
      font-weight: 300;
      color: #a0a0a0;
      white-space: nowrap;
      overflow: hidden;
      text-overflow: ellipsis;
    }

    .arcticle-author {
      font-size: 8px;
      color: #999;
    }
  }
}

.loading-text {
  width: 100%;
  text-align: center;
  padding: 20px;
  font-size: 14px;
  color: #999;
}
</style>